//
//  ZhiShiDetailViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/6/23.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "ZhiShiDetailViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "UIImageView+WebCache.h"

#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "HYActivityView.h"
#import "ReportDetailVC.h"
#import "PointsManager.h"
#import "zhiShi.h"

@interface ZhiShiDetailViewController (){
    
    float screenWidth;
    float screenHeight;
    UIButton *rightBtn;
}
@property (nonatomic, strong) HYActivityView *activityView;
@end

@implementation ZhiShiDetailViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.title=@"小知识";
    
    screenWidth=[UIScreen mainScreen].bounds.size.width;
    screenHeight=[UIScreen mainScreen].bounds.size.height;
    _zhiShiWebView=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-64)];
    _zhiShiWebView.delegate=self;
    [self.view addSubview:_zhiShiWebView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(0, 0, 25, 20);
    [rightBtn setImage:[UIImage imageNamed:@"line"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(more) forControlEvents:UIControlEventTouchUpInside];
    rightBtn.userInteractionEnabled=NO;
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    [self getZhiShiInfo];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
}

-(void)getZhiShiInfo{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",self.itemid] forKey:@"itemid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"convenientdetail.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"convenientdetail"]) {
            
            NSDictionary *dic=[result objectForKey:@"convenientinfo"];
            self.htmlString=[NSString stringWithFormat:@"<html><body><h3 align='center'>%@</h3><p align='center'>%@",[dic objectForKey:@"title"],[dic objectForKey:@"content"]];
            [self.zhiShiWebView loadHTMLString:self.htmlString baseURL:nil];
            self.titleLab=[dic objectForKey:@"title"];
            self.share_url=[dic objectForKey:@"share_url"];
            rightBtn.userInteractionEnabled=YES;
            
        }else{
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = @"获取详情失败!";
            [hud hide:YES afterDelay:1];
        }
        
    }failureAction:^{
        
    }];
    
}

-(void)more{
    
    if ([Config isShield]) {
        [self showAlertWithMessage:@"您已被村联络员屏蔽在该村的发言。如需申诉,请到“我-消息-通讯录”中与联络员进行联系"];
        return;
    }
    
    [self.view endEditing:YES];
    __weak typeof(self) weakSelf = self;
    
    if (!self.activityView) {
        self.activityView = [[HYActivityView alloc]initWithTitle:@"分享到" referView:self.view];
        
        //横屏会变成一行6个, 竖屏无法一行同时显示6个, 会自动使用默认一行4个的设置.
        self.activityView.numberOfButtonPerLine = 6;
        
        ButtonView *bv = [[ButtonView alloc]initWithText:@"QQ" image:[UIImage imageNamed:@"cunli_qqFriend"] handler:^(ButtonView *buttonView){
            NSLog(@"点击QQ");
            
            [PointsManager sendSharePointWithChannel:@"4" andItemid:[NSString stringWithFormat:@"%d",self.itemid]];
            
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qqData.title = self.titleLab;
            
            [UMSocialData defaultData].extConfig.qqData.url = self.share_url;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQQ] content:self.share_url image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    NSLog(@"QQ分享成功");
                    
                    [PointsManager sendSharePointWithChannel:@"4" andItemid:[NSString stringWithFormat:@"%d",self.itemid]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"QQ空间" image:[UIImage imageNamed:@"cunli_qqZone"] handler:^(ButtonView *buttonView){
            //            NSLog(@"点击QQ空间");
            
            [UMSocialData defaultData].extConfig.qqData.qqMessageType = UMSocialQQMessageTypeDefault;
            [UMSocialData defaultData].extConfig.qzoneData.title = self.titleLab;
            [UMSocialData defaultData].extConfig.qzoneData.url = self.share_url;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToQzone] content:self.titleLab image:[UIImage imageNamed:@"aboutApp01.png"] location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                if (response.responseCode == UMSResponseCodeSuccess) {
                    //                    NSLog(@"QQ空间分享成功");
                    
                    [PointsManager sendSharePointWithChannel:@"3" andItemid:[NSString stringWithFormat:@"%d",self.itemid]];
                }
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"微信" image:[UIImage imageNamed:@"cunli_wechatFriend"] handler:^(ButtonView *buttonView){
            
            //            NSLog(@"点击微信");
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatSessionData.url = self.share_url;
            [UMSocialData defaultData].extConfig.title = self.titleLab;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatSession] content:self.titleLab image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //                NSLog(@"分享成功");
                
                [PointsManager sendSharePointWithChannel:@"1" andItemid:[NSString stringWithFormat:@"%d",self.itemid]];
            }];
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"朋友圈" image:[UIImage imageNamed:@"cunli_wechatTimeline"] handler:^(ButtonView *buttonView){
            //            NSLog(@"朋友圈");
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialData defaultData].extConfig.wechatTimelineData.url = self.share_url;
            [UMSocialData defaultData].extConfig.title = self.titleLab;
            
            [[UMSocialDataService defaultDataService] postSNSWithTypes:@[UMShareToWechatTimeline] content:self.titleLab image:nil location:nil urlResource:nil presentedController:self completion:^(UMSocialResponseEntity *response) {
                //                NSLog(@"分享成功");
                
                [PointsManager sendSharePointWithChannel:@"2" andItemid:[NSString stringWithFormat:@"%d",self.itemid]];
            }];
            
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"收藏" image:[UIImage imageNamed:@"cunli_favorite"] handler:^(ButtonView *buttonView){
            [weakSelf doFavoriteAction];
        }];
        [self.activityView addButtonView:bv];
        
        bv = [[ButtonView alloc]initWithText:@"举报" image:[UIImage imageNamed:@"cunli_report"] handler:^(ButtonView *buttonView){
            [weakSelf doReportAction];
        }];
        [self.activityView addButtonView:bv];
        
    }
    
    [self.activityView show];
}

- (void)doReportAction
{
    
    ReportDetailVC *reportVC = [[ReportDetailVC alloc] init];
    reportVC.itemid = [NSString stringWithFormat:@"%d",self.itemid];
    reportVC.m_itemid = [NSString stringWithFormat:@"%d",self.item_mid];
    [self.navigationController pushViewController:reportVC animated:YES];
}

-(void)doFavoriteAction{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",self.item_mid] forKey:@"item_mid"];
    [CheckParams setObject:[NSString stringWithFormat:@"%d",self.itemid] forKey:@"itemid"];
    [CheckParams setObject:@"1" forKey:@"type"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"add_favorite.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"========%@",result);
        if ([[result objectForKey:@"response"] isEqualToString:@"add_favorite"]) {
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"done"]];
            hud.labelText = @"收藏成功!";
            [hud hide:YES afterDelay:1];
        }else{
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = [[result objectForKey:@"error"] objectForKey:@"text"];
            [hud hide:YES afterDelay:1];
        }
    }failureAction:^{
        
    }];
}

-(void)goBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)showAlertWithMessage:(NSString *)message
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:message delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    [alert show];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
